cap cd "/Users/ddgueorg/Dropbox/C-GAP/C-GAP Replication Files/Coding"
cap cd "C:\Users\ejm5\Dropbox\C-GAP\C-GAP Replication Files\Coding"

clear all
set more off 
*****

**Data Composition**
cd "../Data"


**********************************************************
/* Figure 5.1 trends in local and national consultation */
**********************************************************
preserve
use "participation_master_pid.dta", clear
sort province year
fillin province year
replace count=0 if count==.
egen count_year = sum(count), by(year)
* Including national stats *
gen nat_count=12 if year==2007 
replace nat_count=79 if year==2008
replace nat_count=109 if year==2009
replace nat_count=83 if year==2010
replace nat_count=121 if year==2011
replace nat_count=115 if year==2012

collapse count_year (max) nat_count, by(year)
twoway (connected count_year year if year>1999 & year<2013) (connected nat_count year if year>1999 & year<2013, lcolor(black) msymbol(square) mcolor(black)), xlabel(#7) ///
xtitle(, size(zero) )ytitle(No. of notice-and-comment campaigns) ylabel(, angle(horizontal)) legend(order(1 "Provincial" 2 "National") rows(1) position(11) ring(0))
graph save "../Results/fig_5.1_trends.gph", replace
restore

**********************************************************
**Figures 5.4 Press Freedom, based on third-party data **
**********************************************************
use "Reporter_wo_borders.dta", clear

twoway (connected journalistsimprisoned year) (connected netizensimprisoned year, mcolor(black) msymbol(square) lcolor(black)), ///
ytitle(Number imprisoned) ylabel(, angle(horizontal)) xtitle(, size(zero)) ///
legend(order(1 "Journalists" 2 "Netizens" ) position(11) ring(0))

graph save Graph "../Results/fig_5.4_press_freedom.gph", replace


**********************************************************
/* Merging Comparative Data, based on third-party data ** */
**********************************************************
use "Freedom House.dta", clear

**Kountry**
kountry country, from(other) stuck 
rename _ISO3N_ iso3n
kountry iso3n, from(iso3n) to(cown) 
rename _COWN_ ccode

**Merge with WGI**
merge m:m ccode year using "wgidataset.dta"
drop _merge
**Merge with EIU**
merge m:m ccode year using "EIU-Full.dta"
drop _merge
**Merge with ICRG Quality of Governance**
merge m:m ccode year using "qog_p.dta"
drop _merge

**Combined China Line**
g pr=7 if political<6
replace pr=6 if political>5
replace pr=5 if political>11
replace pr=4 if political>17
replace pr=3 if political>23
replace pr=2 if political>29
replace pr=1 if political>35
replace pr=. if political==.

g cl=7 if civil <8
replace cl=6 if civil>7
replace cl=5 if civil>16
replace cl=4 if civil>25
replace cl=3 if civil>34
replace cl=2 if civil>43
replace cl=1 if civil>52
replace cl=. if civil==.

**Reconstruct Freedom House Variables**
g freedom=(pr+cl)/2
replace freedom=6.5 if ccode==710 & year==2002

**status codes**
encode status, g(status_code)
replace status_code=5 if status_code==3
replace status_code=3 if status_code==4
replace status_code=4 if status_code==5
label define status_code 1 "Authoritarian regimes" 2 "Flawed democracies" 4 "Full democracies" 3 "Hybrid regimes", replace
by ccode, sort: egen status_code_temp=mean(status_code)
by ccode, sort: gen status_code_temp2=round(status_code_temp,1)
replace  status_code = status_code_temp2 
drop status_code_*

**Authoritarain Averages**
by year, sort: egen fh_fotpsc5_aut = mean(fh_fotpsc5) if status_code==1 
replace fh_fotpsc5=100-fh_fotpsc5
replace fh_fotpsc5_aut=100-fh_fotpsc5_aut
by year, sort: egen ti_cpi_aut = mean(ti_cpi) if status_code==1 
by year, sort: egen civil_aut = mean(civil) if status_code==1 
by year, sort: egen political_aut = mean(political) if status_code==1 
by year, sort: egen politicalculture_aut = mean(politicalculture) if status_code==1 
by year, sort: egen politicalparticipation_aut = mean(politicalparticipation) if status_code==1 
by year, sort: egen civilliberties_aut = mean(civilliberties) if status_code==1 


**********************************************************
**Figures 5.2 and 5.3, Comparative Trends, based on third-party data ****
**********************************************************

cd "../Results/"
preserve 
drop if year<2002
drop if year>2011
replace freedom=6.5 if ccode==710 & year==2002
lowess RQ_PRANK year if ccode==710, ylabel(, angle(horizontal)) lineopts(lpattern(solid)) scheme(s1mono) xtitle(Year) ///
ytitle(Index rank) note(, size(zero)) title (Regulatory Quality) name(reg_quality) 
lowess GE_PRANK year if ccode==710, ylabel(, angle(horizontal)) lineopts(lpattern(solid)) scheme(s1mono) xtitle(Year) ///
ytitle(Index rank) note(, size(zero)) title (Government Effectiveness) name(gov_effect) 
lowess freedom year if ccode==710, ylabel(1(1)7, angle(horizontal)) lineopts(lpattern(solid)) scheme(s1mono) xtitle(Year) /// 
note(, size(zero)) title (Freedom House Index, size(msmall)) ytitle(Freedom index) name(freedom)
lowess p_polity2 year if ccode==710, ylabel(-8(.4)-6, angle(horizontal)) lineopts(lpattern(solid)) scheme(s1mono) xtitle(Year) ///
note(, size(zero)) title(Polity IV Index, size(msmall)) ytitle(Polity index) name(polity)
lowess political year if ccode==710 & year>2002,  lineopts(lpattern(solid)) scheme(s1mono) xtitle(Year) ///
ylabel(, angle(horizontal)) note(, size(zero)) title(FH Political Rights, size(msmall)) ytitle(Political rights index) name(political)
lowess civil year if ccode==710,  lineopts(lpattern(solid)) scheme(s1mono) xtitle(Year) ///
ylabel(, angle(horizontal)) note(, size(zero)) title(FH Civil Liberties, size(msmall)) ytitle(Civil liberties index) name(civil)

graph combine freedom polity gov_effect reg_quality 
graph save "fig_5.2_Dem_Governance.gph", replace

graph combine political civil
graph save "fig_5.3_Political_Civil.gph", replace
restore


**********************************************************
**Figures 5.5 and 5.7, China in Comparison, based on third-party data ****
**********************************************************

lowess politicalparticipation year if ccode==710,  lineopts(lpattern(solid)) scheme(s1mono) xtitle(, size(zero)) note(, size(zero)) ///
ylabel(, angle(horizontal)) ytitle(Political participation index) title(EIU political participation, size(msmall)) name(EIU_pp)
lowess politicalculture year if ccode==710,  lineopts(lpattern(solid)) scheme(s1mono) xtitle(, size(zero)) note(, size(zero)) ///
ylabel(, angle(horizontal))  ytitle(Political culture index) title(EIU political culture, size(msmall)) name(EIU_pc)

graph combine EIU_pc EIU_pp
graph save "fig_5.5_culture_participation.gph", replace


**Figure 5.6 Comparative Democracy Density Graphs**
twoway (kdensity overallscore, lpattern(dash)) (kdensity politicalparticipation) if year==2011 ///
, ytitle(Density) xtitle(Index) ylabel(, angle(horizontal)) xline(3.89, lcolor(black) lpattern(dash)) xline(3.14, lcolor(gs3) ///
lpattern(dot)) title("All Regimes") xtitle(,size(0)) legend(order(1 "EIU Democracy Index" 2 "EIU Participation Index") ) scheme(s1mono) name(EIU)

twoway (kdensity overallscore, lpattern(dash)) (kdensity politicalparticipation) if status_code==1 & year==2011  ///
, ytitle(Density) xtitle(Index) ylabel(,angle(horizontal)) xline(3.89, lcolor(black) lpattern(dash)) xline(3.14, lcolor(gs3) ///
lpattern(dot)) title("Authoritarian Regimes") xtitle(,size(0)) legend(order(1 "EIU Democracy Index" 2 "EIU Participation Index")) scheme(s1mono) name(EIU_Authoritarian)

grc1leg EIU EIU_Authoritarian, name(EIU_Combined)

twoway (kdensity political, lpattern(dash)) (kdensity civil) if year==2011 ///
, ytitle(Density) xtitle(Index) ylabel(, angle(horizontal)) xline(15, lcolor(black) lpattern(dash)) xline(2, lcolor(gs3) ///
lpattern(dot)) title("All Regimes") xtitle(,size(0)) legend(order(1 "FH Political Rights" 2 "FH Civil Society") ) scheme(s1mono) name(FH)

twoway (kdensity political, lpattern(dash)) (kdensity civil) if status_code==1 & year==2011  ///
, ytitle(Density) xtitle(Index) ylabel(, angle(horizontal)) xline(15, lcolor(black) lpattern(dash)) xline(2, lcolor(gs3) ///
lpattern(dot)) title("Authoritarian Regimes") xtitle(,size(0)) legend(order(1 "FH Political Rights" 2 "FH Civil Society")) scheme(s1mono) name(FH_Authoritarian)

grc1leg FH FH_Authoritarian, name(FH_Combined)

graph combine FH_Combined EIU_Combined, rows(2)
graph save Graph "fig_5.6_Comparative_dem_part.gph", replace


** Figure 5.7 Governance Density Graphs**
twoway (kdensity GE_EST, xtitle(Index)) if status_code==1 &  year==2011,  ytitle(Density) ///
xline(.1193766, lpattern(dash)) ylabel(, angle(horizontal))title("WGI government effectiveness", size(medium)) ///
legend(off) scheme(s1mono) name(WGI_gov_density)

twoway (kdensity RQ_EST, xtitle(Index)) if status_code==1 & year==2011, ///
ytitle(Density) ylabel(, angle(horizontal)) xline(-.2031787 , lpattern(dash)) title("WGI Regulatory quality", size(medium)) ///
legend(off) scheme(s1mono) name(WGI_req_density)

twoway (kdensity functioningofgovernment, xtitle(Index)) if status_code==1 & year==2011  ///
, ytitle(Density) ylabel(, angle(horizontal)) xtitle(Index)  ///
xline(5, lpattern(dash)) title("EIU functioning government", size(medium)) ///
legend(off) scheme(s1mono) name(EIU_gov_density)

twoway (kdensity icrg_qog, xtitle(Index)) if status_code==1 & year==2011, ytitle(Density) ///
xline(.4791667, lpattern(dash)) title("ICRG quality of governance", size(medium)) ///
legend(off) scheme(s1mono) name(icrg_density)

graph combine WGI_gov_density WGI_req_density EIU_gov_density  icrg_density

graph save Graph "fig_5.7_Comparative_gov.gph", replace



